package com.superview.power;

import com.superview.util.Utility;

public class CmdFactory {
	public static final byte CMD_GET_IO_OUT_VALUE = 1;
	public static byte getCmdGetIoOutValue() {
		return CMD_GET_IO_OUT_VALUE;
	}

	public byte getTempOutput() {
		return tempOutput;
	}

	public static final byte CMD_GET_IO_IN_VALUE = 2;
	public static final byte CMD_SET_IO_OUT_VALUE = 3;
	public static final byte CMD_GET_IO_NAME = 4;
	public static final byte CMD_SET_IO_NAME = 5;
	public static final byte CMD_GET_TIMING_INFO = 6;
	public static final byte CMD_SET_TIMING_INFO = 7;
	public static final byte CMD_GET_RTC_VALUE = 8;
	public static final byte CMD_SET_RTC_VALUE = 9;
	public static final byte CMD_SET_TIMING_ON_MSK = 10;
	public static final byte CMD_GET_TIMING_ON_MSK = 11;
	public static final byte CMD_SET_INPUT_CTL_ON_MSK = 12;
	public static final byte CMD_GET_INPUT_CTL_ON_MSK = 13;
	public static final byte CMD_SET_SIG_IO_SOME_BIT = 36;
	public static final byte CMD_SET_IP_CONFIG = 33;
	public static final byte CMD_GET_IP_CONFIG = 34;
	public static final byte CMD_GET_INPUT_CTL_MODE_INDEX = 40;
	public static final byte CMD_SET_INPUT_CTL_MODE_INDEX = 41;

	static private short cmd_index = 0;
	// 当前命令好
	private byte tempCmdCode;
	// 当前命令索引
	private Short tempCmdIndex;
	// 当前输入端口号
	private byte tempOutput;

	public CmdFactory(byte cmdCode, byte output) {
		tempCmdIndex = getCmd_index();
		tempCmdCode = cmdCode;
		tempOutput = output;
	}

	public Short getTempCmdIndex() {
		return tempCmdIndex;
	}

	public static short getCmd_index() {

		if (cmd_index >= Short.MAX_VALUE) {
			cmd_index = 0;
		}
		cmd_index += 1;
		return cmd_index;
	}

	public short getMainBodyLength(byte cmdCode) {
		if (CMD_SET_IO_OUT_VALUE == cmdCode) {
			return 5;
		} else if (CMD_SET_SIG_IO_SOME_BIT == cmdCode) {
			return 4;
		} else if (CMD_GET_IO_OUT_VALUE == cmdCode) {
			return 0;
		}

		return -1;
	}

	public byte[] makeRequestCmdHead(byte cmdCode) {

		byte[] heads = new byte[7];
		// 命令号
		// sb.append(String.format("%1$#2x", cmdCode)).append(",");
		heads[0] = cmdCode;

		// 命令索引(流水号)，一般从0开始递增。
		byte[] indexs = Utility.short2Byte(tempCmdIndex);
		// sb.append(String.format("%1$#2x",
		// indexs[1])).append(",").append(String.format("%1$#2x",
		// indexs[0])).append(",");
		heads[1] = indexs[1];
		heads[2] = indexs[0];
		// 命令选项，表示报文是否正确，或者表示报文状态
		byte cmdOption = 0;
		// sb.append(String.format("%1$#2x", cmdOption)).append(",");
		heads[3] = cmdOption;

		// 正文长度，字节为单位
		Short cmdLength = getMainBodyLength(cmdCode);
		byte[] cmdLengths = Utility.short2Byte(cmdLength);
		// sb.append(String.format("%1$#2x",
		// cmdLengths[1])).append(",").append(String.format("%1$#2x",
		// cmdLengths[0])).append(",");
		heads[4] = cmdLengths[1];
		heads[5] = cmdLengths[0];

		// 正文校验和,正文数据累加的结果,由于走的是TCP协议，可以忽略此项，以0填充
		byte dataCheckSum = 0;
		// sb.append(String.format("%1$#2x", dataCheckSum)).append(",");
		heads[6] = dataCheckSum;
		return heads;

	}

	public byte[] make_CMD_SET_IO_OUT_VALUE_request_message(byte output) {
		// StringBuilder sb = new StringBuilder();
		// sb.append(makeRequestCmdHead(this.CMD_SET_IO_OUT_VALUE));
		//		
		// //io_count，IO的数量
		// sb.append(String.format("%1$#2x", outputCount)).append(",");
		//		
		// //io_value[4]，IO的状态值,1表示闭合，0表示打开
		// sb.append(String.format("%1$#2x", outputIndex)).append(",");
		// sb.append(String.format("%1$#2x", outputValue1)).append(",");
		// sb.append(String.format("%1$#2x", outputValue2)).append(",");
		// sb.append(String.format("%1$#2x", outputValue3));

		byte[] head = makeRequestCmdHead(CMD_SET_IO_OUT_VALUE);
		byte[] ms = new byte[7 + this.getMainBodyLength(CMD_SET_IO_OUT_VALUE)];

		ms[0] = head[0];
		ms[1] = head[1];
		ms[2] = head[2];
		ms[3] = head[3];
		ms[4] = head[4];
		ms[5] = head[5];
		ms[6] = head[6];

		// io_count，IO的数量
		ms[7] = 16;

		byte[] outputs = Utility.short2Byte((short) (1 << output - 1));
		// System.out.println("outputs.length"+outputs.length);
		ms[8] = outputs[1];
		ms[9] = outputs[0];
		ms[10] = 0;
		ms[11] = 0;

		// byte[] outputs = Utility.short2Byte((short)(1<<7));
		// ms[7]=outputs[1];
		// ms[8]=outputs[0];
		// ms[9]=0;
		// ms[10]=0;

		// return "0x3,0x25,0x0,0x0,0x5,0x0,0x0,0x8,0x2,0x0,0x0,0x0";
		return ms;
		// return sb.toString();

	}

	public byte[] make_CMD_SET_SIG_IO_SOME_BIT_request_message(byte output) {
		// StringBuilder sb = new StringBuilder();
		// sb.append(makeRequestCmdHead(this.CMD_SET_IO_OUT_VALUE));
		//		
		// //io_count，IO的数量
		// sb.append(String.format("%1$#2x", outputCount)).append(",");
		//		
		// //io_value[4]，IO的状态值,1表示闭合，0表示打开
		// sb.append(String.format("%1$#2x", outputIndex)).append(",");
		// sb.append(String.format("%1$#2x", outputValue1)).append(",");
		// sb.append(String.format("%1$#2x", outputValue2)).append(",");
		// sb.append(String.format("%1$#2x", outputValue3));

		byte[] head = makeRequestCmdHead(CMD_SET_SIG_IO_SOME_BIT);
		byte[] ms = new byte[7 + this
				.getMainBodyLength(CMD_SET_SIG_IO_SOME_BIT)];

		ms[0] = head[0];
		ms[1] = head[1];
		ms[2] = head[2];
		ms[3] = head[3];
		ms[4] = head[4];
		ms[5] = head[5];
		ms[6] = head[6];

		// io_count，IO的数量
		ms[7] = 16;

		byte[] outputs = Utility.short2Byte((short) (1 << output - 1));
		ms[7] = outputs[1];
		ms[8] = outputs[0];
		ms[9] = 0;
		ms[10] = 0;

		// return "0x3,0x25,0x0,0x0,0x5,0x0,0x0,0x8,0x2,0x0,0x0,0x0";
		return ms;
		// return sb.toString();

	}

	public byte[] getRequestMessage() {
		byte[] message = null;
		if (CMD_SET_IO_OUT_VALUE == tempCmdCode) {
			message = this
					.make_CMD_SET_IO_OUT_VALUE_request_message(tempOutput);
		} else if (CMD_SET_SIG_IO_SOME_BIT == tempCmdCode) {
			message = this
					.make_CMD_SET_SIG_IO_SOME_BIT_request_message(tempOutput);
		}
		return message;
	}

	public static void main(String[] args) {
		System.out.println(1 << 15);
		// byte[] outputs = new byte[2];
		// outputs[0]=1;
		// outputs[1]=2;
		// outputs[2]=3;
		// outputs[3]=4;
		 SocketFactory.request(CmdFactory.CMD_SET_IO_OUT_VALUE, (byte) 0);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 1);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 2);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 3);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 4);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 5);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 6);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 7);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 8);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 9);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 10);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 11);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 12);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 13);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 14);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 15);
		SocketFactory.request(CmdFactory.CMD_SET_SIG_IO_SOME_BIT, (byte) 16);
		// // CmdFactory cf = new CmdFactory();
		// System.out.println("cmd message = "+cf.make_CMD_SET_IO_OUT_VALUE_request_message((byte)8,
		// (byte)4, (byte)0, (byte)0, (byte)0));
	}

}
